package com.hxkcloud.patient.activitys;

import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hxkcloud.patient.R;
import com.hxkcloud.patient.adapter.FragmentSearchRecordAdapter;
import com.hxkcloud.patient.beans.SearchRecordBean;
import com.hxkcloud.patient.beans.SearchRecordBeanList;
import com.hxkcloud.patient.fragments.FinishFragment;
import com.hxkcloud.patient.request.MessageCode;
import com.hxkcloud.patient.request.YiJiaHuNetWorkUtils;

import java.util.ArrayList;
import java.util.List;

public class SearchRecordActivity extends BaseFragmentActivity implements View.OnClickListener{
    private LinearLayout ll_title;
    private ImageView iv_back;
    private TextView tv_title;
    private TextView tv_collect;


    private ViewPager viewPager;

    private LinearLayout mTabChat;
    private LinearLayout mTabFound;
    private TextView chat;
    private TextView found;
    private ImageView mTabLine;
    private int screenWidth;
    private FragmentSearchRecordAdapter fragmentAdapter;
    List<Fragment> fragments ;
    FinishFragment finishFragment;
    FinishFragment cancleFragment;
   // String [] titls = {"已完成(12)","未完成(15)"};
    private int cancelCount;
    private int completedCount;
    private SearchRecordBeanList searchRecordBeanList;
    private List<SearchRecordBean> searchRecordBean;
    @Override
    protected void updateByHandler(Message msg) {
        switch (msg.what){
            case MessageCode.NETWORKCODE_USERRECORD_SUCCESS:

                searchRecordBeanList = (SearchRecordBeanList) msg.obj;
                if (searchRecordBeanList!=null){
                    searchRecordBean = searchRecordBeanList.getCancellist();
                    cancelCount = searchRecordBeanList.getCancelCount();
                    completedCount = searchRecordBeanList.getCompletedCount();
                    chat.setText("已完成("+completedCount+")");

                    found.setText("已取消("+cancelCount+")");

                }
               // if ()
                break;
            case MessageCode.NETWORKCODE_USERRECORD_FAILURE:

                break;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_record);
        fragments = new ArrayList<Fragment>();
        initView();
        initData();
    }

    private void initData() {
        fragmentAdapter = new FragmentSearchRecordAdapter(getSupportFragmentManager(),fragments);
        viewPager.setAdapter(fragmentAdapter);
        viewPager.setOnPageChangeListener(new TabOnPageChangeListener());
        initTabLine();

      //  loading();
        YiJiaHuNetWorkUtils.UserRecord(loginInfo.getUserId(),"0","0",1,10,handler);
    }


    private void initView() {
        ll_title = (LinearLayout) findViewById(R.id.ll_title);
        iv_back = (ImageView) findViewById(R.id.iv_title_back);
        tv_title = (TextView) findViewById(R.id.tv_title_title);
        tv_collect = (TextView) findViewById(R.id.tv_title_func);

        setTitlePadding(ll_title);
        iv_back.setVisibility(View.VISIBLE);
        iv_back.setOnClickListener(this);
        tv_title.setText("咨询记录");


        viewPager = (ViewPager)findViewById(R.id.pagetabstrip_vp);
        chat=(TextView) findViewById(R.id.id_chat);
        found=(TextView) findViewById(R.id.id_found);

        chat.setOnClickListener(new TabOnClickListener(0));
        found.setOnClickListener(new TabOnClickListener(1));
        finishFragment = new FinishFragment(0,0);
        cancleFragment = new FinishFragment(-1,1);
        fragments.add(finishFragment);
        fragments.add(cancleFragment);

        mTabChat=(LinearLayout) findViewById(R.id.id_tab1_chat);
        mTabFound=(LinearLayout) findViewById(R.id.id_tab2_found);


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_title_back:
                finish();
                break;


        }
    }
    private void initTabLine() {
        mTabLine=(ImageView) findViewById(R.id.id_tab_line);
        //获取屏幕的宽度
        DisplayMetrics outMetrics=new DisplayMetrics();
        getWindow().getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
        screenWidth=outMetrics.widthPixels;

        //获取控件的LayoutParams参数(注意：一定要用父控件的LayoutParams写LinearLayout.LayoutParams)
        LinearLayout.LayoutParams lp=(android.widget.LinearLayout.LayoutParams) mTabLine.getLayoutParams();
        lp.width=screenWidth/2;//设置该控件的layoutParams参数
        mTabLine.setLayoutParams(lp);//将修改好的layoutParams设置为该控件的layoutParams

    }
    public  class TabOnClickListener implements View.OnClickListener{
        private int index=0;
        public TabOnClickListener(int i){
            index=i;
        }
        @Override
        public void onClick(View v) {
            viewPager.setCurrentItem(index);//选择某一页
        }
    }
    class TabOnPageChangeListener implements ViewPager.OnPageChangeListener{
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            LinearLayout.LayoutParams lp=(android.widget.LinearLayout.LayoutParams) mTabLine.getLayoutParams();
            //返回组件距离左侧组件的距离
            lp.leftMargin= (int) ((positionOffset+position)*screenWidth/2);
            mTabLine.setLayoutParams(lp);
        }

        @Override
        public void onPageSelected(int position) {
            //重置所有TextView的字体颜色
            resetTextView();
            switch (position) {
                case 0:
                    chat.setTextColor(getResources().getColor(R.color.light_blue));
                    break;
                case 1:
                    found.setTextColor(getResources().getColor(R.color.light_blue));
                    break;
            }
        }
        @Override
        public void onPageScrollStateChanged(int state) {
        }
    }
    private void resetTextView() {
        chat.setTextColor(getResources().getColor(R.color.black_text));
        found.setTextColor(getResources().getColor(R.color.black_text));
    }
}
